import http from "@/api/services";
import sysUrl from "./sysUrl";
import { ResPage, ResultData, SaveListRes } from "@/api/types";
import { ExportObj } from "../types/base";

class ApiService {
  /**
   * 获取服务器时间.
   * @returns 服务器时间
   */
  time<T>(): Promise<ResultData<T>> {
    return http.get("/data/time");
  }

  /**
   * 获取服务器配置版本，用于前后端版本不匹配情况下，强制进行页面刷新.
   * @returns 服务器配置版本
   */
  version<T>(): Promise<ResultData<T>> {
    return http.get("/data/version");
  }

  /**
   * 分组查询，处理日期类型
   * @param {序列关键字} key
   */
  group<T>(tableName, condition): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_GROUP + tableName, condition);
  }

  /**
   * 分组查询，处理日期类型
   * @param {序列关键字} key
   */
  groupNoLoading<T>(tableName, condition): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_GROUP + tableName, condition, { loading: false });
  }

  /**
   * 获取分类.
   * @param {*请求URL} url
   * @param {*查询条件，可不传} condition
   * @param {*是否需要缓存，默认缓存} isCahce
   */
  findClassify<T>(key, loading = true): Promise<ResultData<T>> {
    let param = {
      condition: { csName: key },
      options: { sort: { sort: 1 } }
    };
    return http.post<T>(sysUrl.BASE_FIND + "param_exts", param, { loading: loading });
  }

  /**
   * 获取分类.
   * @param {*请求URL} url
   * @param {*查询条件，可不传} condition
   * @param {*是否需要缓存，默认缓存} isCahce
   */
  findParam<T>(key, loading = true): Promise<ResultData<T>> {
    return http.post<T>(sysUrl.BASE_FINDONE + "params", { csName: key }, { loading: loading });
  }

  /**
   * 查询数据.
   * @param {*请求URL} url
   * @param {*查询条件，可不传} condition
   * @param {*是否需要缓存，默认缓存} isCahce
   */
  find<T>(tableName, condition = {}, options = {}, loading = true): Promise<ResultData<T>> {
    let param = {
      condition: condition,
      options: options
    };
    return http.post(sysUrl.BASE_FIND + tableName, param, { loading: loading });
  }

  /**
   * 查询数据.
   * @param {*请求URL} url
   * @param {*查询条件，可不传} condition
   */
  findOne<T>(tableName, condition = {}, loading = true): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_FINDONE + tableName, condition, { loading: loading });
  }

  /**
   * 查询数据.
   * @param {*请求URL} url
   * @param {*查询条件，可不传} condition
   * @param {*是否需要缓存，默认缓存} isCahce
   */
  findNoCancel<T>(tableName, condition = {}, options = {}, loading = true): Promise<ResultData<T>> {
    let param = {
      condition: condition,
      options: options
    };
    return http.post(sysUrl.BASE_FIND + tableName, param, { cancel: false, loading: loading });
  }

  /**
   * 分页查询.
   * @param tableName 模型名称
   * @param condition 条件
   * @returns 结果
   */
  findPage<T>(tableName, condition, options, loading = true): Promise<ResultData<ResPage<T>>> {
    let pageParam = {
      condition: condition,
      options: options
    };
    return http.post(sysUrl.BASE_FIND_PAGE + tableName, pageParam, { loading: loading });
  }

  /**
   * 分页查询.
   * @param tableName 模型名称
   * @param condition 条件
   * @returns 结果
   */
  findTree<T>(tableName, condition = {}, options = {}, loading = true): Promise<ResultData<T>> {
    let pageParam = {
      condition: condition,
      options: options
    };
    return http.post(sysUrl.BASE_FIND_TREE + tableName, pageParam, { loading: loading });
  }

  /**
   * 保存数据.
   * @param {*请求URL} url
   * @param {*保存数据，支持单个和数组} doc
   */
  save<T>(tableName, doc, loading = true): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_SAVE + tableName, doc, { loading: loading });
  }

  /**
   * 批量保存数据.
   * @param {*请求URL} url
   * @param {*保存数据，支持单个和数组} doc
   */
  saveList<T>(tableName: string, docs: T[], loading = true): Promise<ResultData<SaveListRes<T>>> {
    return http.post(sysUrl.BASE_SAVE_DOCS + tableName, docs, { loading: loading });
  }

  /**
   * 通过_id删除单条记录.
   * @param {*请求URL} url
   */
  deleteById<T>(tableName, id, loading = true): Promise<ResultData<T>> {
    return http.get(sysUrl.BASE_DELETE_ID + tableName + "/" + id, {}, { loading: loading });
  }

  /**
   * 通过条件批量删除.
   * @param {*请求URL} url
   * @param {*删除条件} condition
   */
  deleteByCondition<T>(tableName, condition, loading = true): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_DELETE + tableName, condition, { loading: loading });
  }

  /**
   * 通过_id更新数据.
   * @param {*请求URL} url
   * @param {*新数据} doc
   */
  updateById<T>(tableName, doc, loading = true): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_UPDATE + tableName + "/" + doc._id, doc, { loading: loading });
  }

  /**
   * 通过条件更新数据.
   * @param {*请求URL} url
   * @param {*新数据} doc
   */
  updateDocs<T>(tableName, where, doc, loading = true): Promise<ResultData<T>> {
    let params = {
      where: where,
      doc: doc
    };
    return http.post(sysUrl.BASE_UPDATES + tableName, params, { loading: loading });
  }

  /**
   * 查询总数.
   * @param {*表名} tableName
   * @param {*条件} condition
   */
  findCount<T>(tableName, condition, loading = true): Promise<ResultData<T>> {
    return http.post(sysUrl.BASE_FINDCOUNT + tableName, condition, { loading: loading });
  }

  /**
   * 获取自增长序号.
   * @param {*序列KEY} key
   */
  nextValue<T>(key, loading = true): Promise<ResultData<T>> {
    return http.post(sysUrl.SYS_NEXT_VALUE + key, {}, { loading: loading });
  }

  /**
   * 取消自增长序号.
   * @param {*序列KEY} key
   */
  reNextValue<T>(key): Promise<ResultData<T>> {
    return http.post(sysUrl.SYS_RENEXT_VALUE + key);
  }

  // 导出数据
  export(url, condition): Promise<BlobPart> {
    return http.download(sysUrl.BASE_EXPORT_FILE + url, condition);
  }

  // 通用导出
  commonExport(url, params: ExportObj): Promise<BlobPart> {
    return http.postDownload(sysUrl.BASE_EXPORT_COMMON, params);
  }

  // 通用导入
  commonImport(params: FormData): Promise<ResultData> {
    return http.post(sysUrl.BASE_IMPORT_COMMON, params);
  }

  // 下载文件
  download(url): Promise<BlobPart> {
    return http.download(url);
  }
}

export default new ApiService();
